//
//  LeftController.m
//  LeftMenu
//
//  Created by iMac on 17/4/24.
//  Copyright © 2017年 zws. All rights reserved.
//

#import "LeftController.h"
#import "UIViewController+MMDrawerController.h"
#import "ZJSetVC.h"
#import "ZJAdviserVC.h"
#import "ZJExtensionVC.h"

@interface LeftController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic)ZJUserModel * model;
@property (strong, nonatomic)UITableView * tableview;
@end

@implementation LeftController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self updata];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upLeftViewData:) name:@"upLeftViewData" object:nil];
}
-(void)upLeftViewData:(NSNotification *)notification{
    self.model = notification.object[@"model"];
    [self updata];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"upLeftViewData" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self updata];
}
-(void)updata{
    
    self.model = GET_USERINFO;
    self.view.backgroundColor = [UIColor clearColor];
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.image = [UIImage imageNamed:kHeadImgName];
    [self.view addSubview:imageview];
    
    
    UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-100, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    tableview.dataSource = self;
    tableview.delegate  = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.backgroundColor = [UIColor clearColor];
    self.tableview = tableview;
    [self.view addSubview:tableview];
    [self.tableview reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Identifier = @"Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:20.0f];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的顾问";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"设置";
    } return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZJTabBarController* nav = (ZJTabBarController*)self.mm_drawerController.centerViewController;
    UINavigationController *nvc =  nav.viewControllers[0];
    NSDictionary * dict = self.model.consultant;
    if (indexPath.row == 0) {
        
        if (dict[@"mobile"]) {
            ZJAdviserVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJAdviserVC"];
            vc.dict = dict;
            [nvc pushViewController:vc animated:NO];
        }else{
            ZJExtensionVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJExtensionVC"];
//            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            vc.type = NO;
            [nvc presentViewController:vc animated:YES completion:nil];
        }
    }
    
    if (indexPath.row == 1) {
        ZJSetVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSetVC"];
        [nvc pushViewController:vc animated:NO];

    }
    [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
        
        [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
        
    }];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 180)];
    view.backgroundColor = [UIColor clearColor];
    
    
//    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    imageButton.frame = CGRectMake(CGRectGetWidth(view.frame)/2-25, 40, 50, 50);
//    imageButton.layer.cornerRadius = 25;
    UIImageView * img = [[UIImageView alloc]init];
    img.frame = CGRectMake(CGRectGetWidth(view.frame)/2-25, 40, 50, 50);
    img.layer.cornerRadius = 25;
    [img sd_setImageWithURL:[NSURL URLWithString:self.model.avatar] placeholderImage:[UIImage imageNamed:KUserHeadPhoto]];
//    [imageButton setBackgroundImage:img.image forState:UIControlStateNormal];
    [view addSubview:img];
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(img.frame)+5, tableView.bounds.size.width, 20)];
    if ([self.model.real_name isEqualToString:@""]) {
        nameLabel.text = self.model.mobile;
    }else{
    nameLabel.text = self.model.real_name;
    }
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:nameLabel];
    
    return view;
}








@end
